package com.useeinfo.oa.open.dashboard.security;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.security.biz.SystemModuleBiz;
import com.useeinfo.oa.modules.security.entity.SystemModule;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/web/admin/systemModule")
public class SystemModuleAction extends BaseAction {

    @Autowired
    private SystemModuleBiz systemModuleBiz;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getSystemModuleListPage")
    public ModelAndView getSystemModuleListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/system/systemModule/systemModuleList");

        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getSystemModuleListJSON")
    @ResponseBody
    public JSONObject getSystemModuleListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                              @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                              @RequestParam(value = "orderBy", required = false) String orderBy,
                                              @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("moduleNameLike", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return systemModuleBiz.findJSONList(queryParam);
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getSystemModuleEditPage")
    public ModelAndView getSystemModuleEditPage(@RequestParam(value = "moduleId", required = false) String moduleIdParam) {

        Long moduleId = StringConverters.ToLong(moduleIdParam);

        SystemModule systemModule = null;
        if (moduleId != null) {
            systemModule = systemModuleBiz.findModel(moduleId);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/system/systemModule/systemModuleEditPart");
        modelAndView.addObject("systemModule", systemModule);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeSystemModuleEdit")
    @ResponseBody
    public ResultDto executeSystemModuleEdit(SystemModule systemModule, @RequestParam(value = "select_applicationModule[]", required = false) List<String> applicationModules) {

        if (CollectionUtils.isEmpty(applicationModules)) {
            return ResultUtil.error("请选择应用");
        }
        ApplicationModule applicationModule = applicationModuleBiz.findModel(StringConverters.ToLong(applicationModules.get(0)));
        systemModule.setApplicationModuleId(applicationModule != null ? applicationModule.getId() : null);
        if (systemModule.getApplicationModuleId() == null) {
            return ResultUtil.error("请选择应用");
        }
        if (systemModuleBiz.existSystemModuleByapplicationId(applicationModule.getId() + "")) {
            return ResultUtil.error("您输入的模块名称为空或已存在，请重新选择");
        }
        systemModule.setModuleName(applicationModule.getApplicationName());
        systemModule.setModuleDesc(applicationModule.getApplicationName());
        systemModuleBiz.addOrUpdate(systemModule);
        return ResultUtil.success();
    }


    /**
     * 获取系统模块选择数据
     *
     * @param id
     * @param q
     * @return
     */
    @RequestMapping("/getSystemModuleSelect")
    @ResponseBody
    public Set<SelectVO> getSystemModuleSelect(@RequestParam(value = "id", required = false) String id,
                                               @RequestParam(value = "q", required = false) String q) {
        Set<SelectVO> selectVOS = new HashSet<>();
        SystemModule systemModule = systemModuleBiz.findModel(StringConverters.ToLong(id));
        if (systemModule != null) {
            selectVOS.add(new SelectVO(systemModule.getModuleName(), systemModule.getId() + ""));
        }
        QueryParam queryParam = new QueryParam(1, 20);
        queryParam.getSqlMap().put("magicLike", q);
        queryParam.getSqlMap().put("magicId", id);
        List<SystemModule> systemModules = systemModuleBiz.findList(queryParam);
        systemModules.forEach(module -> selectVOS.add(new SelectVO(module.getModuleName(), module.getId() + "")));
        return selectVOS;
    }
}
